package com.fan.configService.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fan.configService.mapper.ApplicationMapper;
import com.fan.configService.mapper.ApplicationUserMapper;
import com.fan.configService.po.Application;
import com.fan.configService.vo.MyPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * java类简单作用描述
 *
 * @Description: java类作用描述
 * @Author: hf
 * @CreateDate: 2018/8/30 9:16
 * @UpdateUser: hf
 * @UpdateDate: 2018/8/30 9:16
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@Service
public class ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    ApplicationEnvService applicationEnvService;
    @Autowired
    ApplicationUserMapper applicationUserMapper;

    public Application getApplicationById(Long appId) {
        return applicationMapper.selectById(appId);
    }

    public Application getApplicationByName(String appName) {
        LambdaQueryWrapper<Application> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Application::getApplicationName, appName);
        Application application = applicationMapper.selectOne(lambdaQueryWrapper);
        return application;
    }


    public IPage<Application> getAllApplicationByPage(MyPage myPage, Long userId) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page page = new Page();
        BeanUtils.copyProperties(myPage, page);
//        LambdaQueryWrapper<Application> lambdaQueryWrapper = Wrappers.lambdaQuery();
//        lambdaQueryWrapper.eq(ApplicationItem::getu, applicationEnvId);
        IPage<Application> iPage = applicationMapper.selectPage(page, null);
        return iPage;
    }


    @Transactional
    public void addApplication(Application application) {
        application.setCreateTime(new Date());
        applicationMapper.insert(application);
        applicationEnvService.createCommonEnv(application.getApplicationId());
    }


    public void delApplication(Long id) {
        applicationMapper.deleteById(id);
    }

    public void updateApplication(Application application) {
        applicationMapper.updateById(application);
    }


}
